﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProjBackEnd.Areas.Crm.Models;
using ProjBackEnd.Lib;
using ProjBackEnd.DAL;

namespace ProjBackEnd.Areas.Crm.Controllers
{
    /// <summary>
    /// 客户信息管理
    /// </summary>
    public class CusOptController : ApiController
    {
        /// <summary>
        /// 获取客户核心信息，主要用于客户检索 eg. :0000000001|COM1|WEB|50|1|ABC
        /// </summary>
        /// <param name="key">用户id|公司代码|平台cd|每页记录数|请求页码|检索关键字</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/crm/cuscore")]
        public RespCusCoreInfo GetCusCore(string key)
        {
            RespCusCoreInfo resp = new RespCusCoreInfo();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 6)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCusKey = strsKeyOriginal[5];
            int iPageSize, iCurrPage;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "分页参数格式错误";
                return resp;
            }
            if (iPageSize > GlobalSettings.MAXPAGESIZE)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "单页记录数量超限";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = crmLogic.GetCusCore(iPageSize, iCurrPage, strComcd, strCusKey);
            return resp;
        }

        /// <summary>
        /// 获取客户基本信息，主要用于展示客户列表 eg. :0000000001|COM1|WEB|50|1|0|0||顺丰
        /// </summary>
        /// <param name="key">用户id|公司代码|平台cd|每页记录数|请求页码|客户分类（0=所有）|客户id（0=所有）|客户助记码（空值=所有）|客户全称（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/crm/cusbaseinfo")]
        public RespCusBaseInfo GetCusBaseInfo(string key)
        {
            RespCusBaseInfo resp = new RespCusBaseInfo();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 9)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCusAlias = strsKeyOriginal[7];
            string strCusnm = strsKeyOriginal[8];
            int iPageSize, iCurrPage, iTagid, iCusid;
            int.TryParse(strsKeyOriginal[5], out iTagid);
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[6], out iCusid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "分页参数格式错误";
                return resp;
            }
            if (iPageSize > GlobalSettings.MAXPAGESIZE)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "单页记录数量超限";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            //暂不开放客户分类逻辑，因此客户分类参数强制传0
            resp = crmLogic.GetCusBaseInfo(iPageSize, iCurrPage, strComcd, iTagid, iCusid, strCusnm, strCusAlias);
            return resp;
        }

        /// <summary>
        /// 获取当前分公司的客户详细信息，主要用于客户信息编辑 eg. :0000000001|COM1|WEB|1
        /// </summary>
        /// <param name="key">用户id|公司代码|平台cd|客户流水号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/crm/cusdetail")]
        public RespCusDetailInfo GetCusDetail(string key)
        {
            RespCusDetailInfo resp = new RespCusDetailInfo();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iCusid;
            int.TryParse(strsKeyOriginal[3],out iCusid);

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = crmLogic.GetCusDetailInfo(strComcd, iCusid);
            return resp;
        }

        /// <summary>
        /// 操作客户详细信息（目前为简化流程，暂不放开审核功能）
        /// </summary>
        /// <param name="_reqInfo">操作请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/crm/cusdetail")]
        public RespCusNullResultInfo UpdateCusDetail(ReqCusDetail _reqInfo)
        {
            RespCusNullResultInfo resp = new RespCusNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = crmLogic.UpdateCusDetail(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.cusdetail);
            return resp;
        }

        /// <summary>
        /// 获取未结算客户列表
        /// </summary>
        /// <param name="key">用户id|公司代码|平台cd|制单人id（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/crm/cusunsettled")]
        public RespCusUnSettled GetCusUnSettled(string key)
        {
            RespCusUnSettled resp = new RespCusUnSettled();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCreateid = strsKeyOriginal[3];
            

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = crmLogic.GetCusUnSettled(strComcd, strCreateid);
            return resp;
        }

    }
}
